package com.qicheng.wlhyspider.common;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OCRUtil {
    public static final String AppKey = "203902660";
    public static final String AppSecret = "DUPa5rxzUcuFnZlHvklRN0qFSlwpGUhB";
    public static final String AppCode = "eda71aa8b73c4cb1bdef5123dfb99485";

    public static final String url = "https://txocr.market.alicloudapi.com/lundear/txocr";

    //自定义参与签名Header前缀（可选,默认只有"X-Ca-"开头的参与到Header签名）
    private final static List<String> CUSTOM_HEADERS_TO_SIGN_PREFIX = new ArrayList<String>();

    public static String execute(String imgBase64) throws Exception {

        Map<String, String> bodys = new HashMap<String, String>();
        String body = HttpUtil.createPost(url)
                .header("Authorization", "APPCODE " + AppCode)
                .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .body("image="+imgBase64)
                .execute()
                .body();
        JSONObject jsonObject = JSONObject.parseObject(body);
        if (jsonObject.getInteger("code") == 0){
            return  jsonObject.getJSONArray("data").getJSONObject(0).getString("words");
        }
        throw new Exception(body);
    }
}
